//!@author denglibin
//!@date 2025/11/3 10:21
//!@desc

mod test {
    use mongodb::bson::{doc, Document};
    use rust_my_tools::app;
    use rust_my_tools::component::mongo;
    use rust_my_tools::component::mongo::mongo_client;
    use rust_my_tools::component::mongo::mongo_client::MongoClient;
    use rust_my_tools::component::mongo::mongo_doc::Book;
    use tiberius::ToSql;
    use tokio::runtime::Builder;

    fn cre_runtime() -> tokio::runtime::Runtime {
        // 创建一个 Tokio 运行时，配置线程池的大小
        Builder::new_multi_thread()
            //.worker_threads(4) // 设置工作线程的数量
            .max_blocking_threads(16) // 设置阻塞任务的最大线程数
            .enable_all()
            .build()
            .unwrap()
    }

    //测试子文件(夹)统计
    #[test]
    pub fn test_connect() {
        let runtime = cre_runtime();
        let s = my_util_lib::date::get_chrono_timestamp_millis();

        runtime.block_on(async {
            app::init_log().await.unwrap();
            mongo::mongo_demo::connect().await.unwrap();
        });
        let e = my_util_lib::date::get_chrono_timestamp_millis();

        println!(" 耗时：{}ms, ", (e - s));
    }

    pub async fn get_mongo_client() -> MongoClient {
        let client_r = mongo_client::MongoClient::new(
            "mongodb://192.168.6.170:27017",
            Some("mongo".to_string()),
            Some("mongo".to_string()),
        )
        .await;
        let client = client_r.unwrap();
        client
    }
    #[test]
    pub fn test_insert_one() {
        let runtime = cre_runtime();
        let s = my_util_lib::date::get_chrono_timestamp_millis();

        runtime.block_on(async {
            let db_name = "my_test_db";
            let collection_name = "my_test_coll";
            let client = get_mongo_client().await;
            let doc = Book {
                _id: 5,
                title: "Cat's Cradle".to_string(),
                author: "Kurt Vonnegut Jr.".to_string(),
            };
            let r = client
                .insert_one_doc(db_name, collection_name, doc)
                .await
                .unwrap();
            println!("插入结果:{}", r);
        });
        let e = my_util_lib::date::get_chrono_timestamp_millis();

        println!(" 耗时：{}ms, ", (e - s));
    }
    #[test]
    pub fn test_insert_many() {
        let runtime = cre_runtime();
        let s = my_util_lib::date::get_chrono_timestamp_millis();

        runtime.block_on(async {
            let db_name = "my_test_db";
            let collection_name = "my_test_coll";
            let client = get_mongo_client().await;
            let docs = vec![
                Book {
                    _id: 8,
                    title: "In Memory of Memory".to_string(),
                    author: "Maria Stepanova".to_string(),
                },
                Book {
                    _id: 9,
                    title: "Pride and Prejudice".to_string(),
                    author: "Jane Austen".to_string(),
                },
            ];
            let r = client
                .insert_many(db_name, collection_name, docs)
                .await
                .unwrap();
            println!("插入结果:{:?}", r);
        });
        let e = my_util_lib::date::get_chrono_timestamp_millis();

        println!(" 耗时：{}ms, ", (e - s));
    }

    #[test]
    pub fn test_query() {
        let runtime = cre_runtime();
        let s = my_util_lib::date::get_chrono_timestamp_millis();

        runtime.block_on(async {
            let db_name = "my_test_db";
            let collection_name = "my_test_coll";
            let client = get_mongo_client().await;

            let query = doc! {};

            client
                .query_list::<Document, _>(db_name, collection_name, query, |doc: Document| {
                    println!("{:?}", doc);
                    true
                })
                .await
                .unwrap();
        });
        let e = my_util_lib::date::get_chrono_timestamp_millis();

        println!(" 耗时：{}ms, ", (e - s));
    }
}
